package org.zsqx.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.zsqx.bean.DisasterUpload;
import org.zsqx.bean.WarnSet;
import org.zsqx.bean.ZdzHoursDataRealt;
import org.zsqx.dao.ZdzReportDAOImpl;
import org.zsqx.dao.ZdzReportDao;
import org.zsqx.tools.PropertiesTools;
import org.zsqx.tools.WordTools;
@Action(value="/warnReportAction",results={
		@Result(name="disasterWarning",location="/login/disasterWarning.jsp"),
		@Result(name="generateWord",location="/login/wordTemp.jsp"),
		@Result(name="soundPage",location="/login/system/warnSoundSet.jsp"),
		@Result(name="input",location="/error.jsp")
})
public class WarnReportAction extends ZdzReportAction {
	public final static String WARN_SOUND = "warnSoundName";
	public static WarnSet ws;
	private List<ZdzHoursDataRealt> zdzList;
	private boolean wind = true;
	private boolean rain = true;
	private String warnSoundName;
	private String warningSound;
	private ZdzHoursDataRealt zdzhdr;
	private String tempString;
	private int listLength;
	/*灾情预警*/
	public String disasterWarning(){
//		zdzList=new ArrayList<ZdzHoursDataRealt>();
		String datetime = rightDatetime();
		PropertiesTools pt = new PropertiesTools();
		if(null == warnSet){
			warnSet = (WarnSet) pt.getPropertyesBean(new WarnSet());
		}
		if(wind)warnSet.setWind("Y");
		if(rain)warnSet.setRain("Y");
		if(warnSet.getOrderbyField()!=null){
			if(warnSet.getOrder()!=null){
				if(warnSet.getOrder().equals("asc")){
					warnSet.setOrder("desc");
				}else{
					warnSet.setOrder("asc");
				}
			}else{
				warnSet.setOrder("asc");
			}
		}else{
			warnSet.setOrder(null);
		}
		zdzList = zdzDao.reportWarn(warnSet,datetime);
		//比较两次的查询结果是否相等,首先比较长度，如果长度一致，再比较id是否相等
		if(zdzList.size()!=0){
			if(zdzList.size()!=listLength){
				//预警声音
				warnSoundName = pt.getPropertiesValue(WARN_SOUND);
			}else{
				List<String> zdzTempList = new ArrayList<String>();
				for(int i=0;i<zdzList.size();i++){
					zdzhdr = zdzList.get(i);
					zdzTempList.add(zdzhdr.getId());
				}
				String tempList[] = tempString.split(",");
				for(int i=0;i<zdzTempList.size();i++){
					String s = zdzTempList.get(i);
					s = s.replaceAll(" ", "");
					if(!tempList[i].equals(s)){
						//预警声音
						warnSoundName = pt.getPropertiesValue(WARN_SOUND);
						break;
					}
				}
			}
		}
		WordTools.zdzList=zdzList;
		ws = warnSet;
		setHourValue();
		WordTools.date=date;
		WordTools.hour=hour.toString();
		
		zdzhdr = new ZdzHoursDataRealt();
		//把zdzList的ID拼成String
		if(zdzList.size()!=0){
			for(int i=0;i<zdzList.size();i++){
				zdzhdr = zdzList.get(i);
				if(i==0){
					tempString = zdzhdr.getId();
					tempString = tempString.replaceAll(" ", "");
				}else{
					tempString = tempString+","+zdzhdr.getId();
					tempString = tempString.replaceAll(" ", "");
				}
			}
		}
		//zdzList的记录数
		listLength = zdzList.size();
		
		return "disasterWarning";	
	}
	public String reportWarnPrev(){
		minute = minute-5;
		if(minute<0){
			minute=55;
			hour-=1;
		}
		return disasterWarning();
	}
	public String reportWarnNext(){
		minute = minute+5;
		if(minute==60){
			minute = 0;
			hour+=1;
		}
		return disasterWarning();
	}
	
	public String generateWord(){
		String time=new SimpleDateFormat("yyyy年MM月dd日 hh小时mm分").format(new Date());
		request.setAttribute("time", time);
		request.setAttribute("date", WordTools.date);
		request.setAttribute("hour", WordTools.hour);
		request.setAttribute("zdzList",WordTools.zdzList);
		return "generateWord";
	}
	//预警声音设置
	public String warnSoundPage(){
		PropertiesTools pt = new PropertiesTools();
		warnSoundName = pt.getPropertiesValue(WARN_SOUND);
		return "soundPage";
	}
	public String warnSoundSet(){
		PropertiesTools pt = new PropertiesTools();
		pt.setPropertiesValue(WARN_SOUND, warnSoundName);
		return warnSoundPage();
	}
	public List<ZdzHoursDataRealt> getZdzList() {
		return zdzList;
	}
	public void setZdzList(List<ZdzHoursDataRealt> zdzList) {
		this.zdzList = zdzList;
	}
	public boolean isWind() {
		return wind;
	}
	public void setWind(boolean wind) {
		this.wind = wind;
	}
	public boolean isRain() {
		return rain;
	}
	public void setRain(boolean rain) {
		this.rain = rain;
	}
	public String getWarnSoundName() {
		return warnSoundName;
	}
	public void setWarnSoundName(String warnSoundName) {
		this.warnSoundName = warnSoundName;
	}
	
	public String getTempString() {
		return tempString;
	}
	public void setTempString(String tempString) {
		this.tempString = tempString;
	}
	public int getListLength() {
		return listLength;
	}
	public void setListLength(int listLength) {
		this.listLength = listLength;
	}
	@Override
	public void validate() {
		if(null!=warnSet&&warnSet.getOrderbyField()!=null&&warnSet.getOrderbyField().equals("")){
			warnSet.setOrderbyField(null);
		}
		super.validate();
	}
}
